import { registerMicroApps, start, addErrorHandler,addGlobalUncaughtErrorHandler } from "qiankun";
import { store } from "../store";
import { dataHelper } from "~/lib_framework";

export default () => {
  const activeRule = "/web5/web4";
  const { host, basePath = "" } = store.getState().config.web4;
  const entry = `${location.protocol}//${host}/${basePath}/`;
  const apps = [
    {
      name: "web4",
      entry, // 默认会加载这个html 解析里面的js 动态的执行 （子应用必须支持跨域）fetch
      container: "#web4",
      activeRule,
      props: { store },
    },
  ];
  registerMicroApps(apps,{
    beforeLoad: app => console.log('基座开始加载', app.name),
  });
  addErrorHandler(err => {
    if(location.pathname.startsWith(activeRule) && err.toString().includes("LOADING_SOURCE_CODE: Failed to fetch")){
      dataHelper.debounce.debounce(()=>{
        alert(`加载【${activeRule}】失败`); // 会连续报两个错误，节流操作一下
      })();
    }  
  });
  addGlobalUncaughtErrorHandler(err => {
      console.log(err);
  });
  start({
    prefetch: false, // 取消预加载
    // fetch :function(url,...args){
    //   console.log("---"+url);
    //   // url=url.replace("http://127.0.0.1:10000","http://127.0.0.1:8000");
    //   // console.log("ssddds"+url);
    //   return window.fetch(url,...args);
    // }
    // sandbox :{
    //   strictStyleIsolation:false,
    //   experimentalStyleIsolation:false
    // },
  });
};
